package com.nativetalk.service.course.search;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.TsNtCategory;
import com.nativetalk.bean.category.TsNtCategoryInfo;
import com.nativetalk.bean.category.TsNtCategoryTeacher;
import com.nativetalk.bean.course.teacher.TdNtTeachCourse;
import com.nativetalk.bean.dict.TsNtDictDeatil;
import com.nativetalk.bean.dict.TsNtDictIndex;
import com.nativetalk.bean.teacher.TdNtTeacherIndex;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.MemcachedKey;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.dao.TdNtTeachCourseDao;
import com.nativetalk.dao.TdNtTeacherDao;
import com.nativetalk.dao.TsNtCourseInfoDao;
import com.nativetalk.dto.ExperienceCourseDto;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.nativetalk.common.util.MemcachedKey.tsNtCategoryIndices;

/**
 * Created by lizhun on 16/1/11.
 */
@Service
public class CourseSearchServiceImpl implements CourseSearchService {
    private static Logger logger = Logger.getLogger(CourseSearchServiceImpl.class);
    @Resource
    private TdNtTeachCourseDao tdNtTeachCourseDao;
    @Resource
    private TdNtTeacherDao tdNtTeacherDao;

    @Resource
    private TsNtCourseInfoDao courseInfoDao;

    @Override
    public RetInfo findCourse(int tea_id, Integer version) {
        String logInfo = this.getClass().getName() + ":findCourse:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            List<TsNtCategoryTeacher> tsNtCategoryTeachers = (List<TsNtCategoryTeacher>) MemcachedUtils.get(MemcachedKey.tsNtCategoryTeachers);
            for (int i = 0; i < tsNtCategoryTeachers.size(); i++) {
                Map<String, Object> map = new HashMap<>();
                map.put("cou_id", tsNtCategoryTeachers.get(i).getCate_id());
                map.put("tea_id", tea_id);
                TdNtTeachCourse tdNtTeachCourse = tdNtTeachCourseDao.findTdNtTeachCourseByCouIdAndTeaId(map);
                if (tdNtTeachCourse == null) {
                    tsNtCategoryTeachers.get(i).setState("1");
                } else {
                    tsNtCategoryTeachers.get(i).setState(tdNtTeachCourse.getTc_state());
                }
            }
            TdNtTeacherIndex tdNtTeacherState = tdNtTeacherDao.findTdNtTeacherIndex(tea_id);
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取教材数据成功成功");
            Map<String, Object> map = new HashMap<>();
            map.put("tsNtCategoryTeachers", tsNtCategoryTeachers);
            map.put("lock", tdNtTeacherState.getTea_islock());
            List<TsNtDictIndex> tsNtDictIndices = new ArrayList<>();
            TsNtDictIndex tsNtDictIndex = new TsNtDictIndex();
            tsNtDictIndex.setDict_id(15);
            tsNtDictIndex.setCode_name("/nativetalk/course-intro-video/intro.mp4");
            tsNtDictIndices.add(tsNtDictIndex);
            TsNtDictIndex tsNtDictIndexTwo = new TsNtDictIndex();
            tsNtDictIndexTwo.setDict_id(15);
            tsNtDictIndexTwo.setCode_name("/nativetalk/course-intro-video/tips.mp4");
            tsNtDictIndices.add(tsNtDictIndexTwo);
            if (version >= 2) {
                map.put("tsNtDictIndices", tsNtDictIndices);
            }
            retInfo.setObj(map);
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(), e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo findCourseByCategoryId(int tea_id, Integer version, Integer categoryId) {
        String logInfo = this.getClass().getName() + ":findCourseByCategoryId:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            //ts_nt_category 表的数据，WHERE is_valid=0 and parent_id!=0
//            List<TsNtCategoryTeacher> tsNtCategoryTeachers = (List<TsNtCategoryTeacher>) MemcachedUtils.get(MemcachedKey.tsNtCategoryTeachers);
//
//            //所有分类的信息，key是父级分类的id
//            Map<String, List<TsNtCategoryIndex>> categoryMap = (Map<String, List<TsNtCategoryIndex>>) MemcachedUtils.get(MemcachedKey.tsNtCategoryIndexsMap);
//            List<TsNtCategoryIndex> tsNtCategoryIndexList = categoryMap.get(categoryId.toString());
//
//            List<TsNtCategoryTeacher> tsNtCategoryTeachersForParentId = new ArrayList<>();
//            for (TsNtCategoryIndex shouldCate : tsNtCategoryIndexList) {
//                for (TsNtCategoryTeacher cate : tsNtCategoryTeachers) {
//                    if (cate.getCate_id().intValue() == shouldCate.getCate_id().intValue()) {
//                        tsNtCategoryTeachersForParentId.add(cate);
//                        break;
//                    }
//                }
//            }
//
//            for (int i = 0; i < tsNtCategoryTeachersForParentId.size(); i++) {
//                Map<String, Object> map = new HashMap<>();
//                map.put("cou_id", tsNtCategoryTeachersForParentId.get(i).getCate_id());
//                map.put("tea_id", tea_id);
//                TdNtTeachCourse tdNtTeachCourse = tdNtTeachCourseDao.findTdNtTeachCourseByCouIdAndTeaId(map);
//                if (tdNtTeachCourse == null) {
//                    tsNtCategoryTeachersForParentId.get(i).setState("1");
//                } else {
//                    tsNtCategoryTeachersForParentId.get(i).setState(tdNtTeachCourse.getTc_state());
//                }
//            }

            // 根据大分类id查询老师开设的课程信息
            List<TsNtCategoryTeacher> tsNtCategoryTeachersForParentId = tdNtTeacherDao.findOpenCourseInfosByCategoryId(tea_id, categoryId);
            TdNtTeacherIndex tdNtTeacherState = tdNtTeacherDao.findTdNtTeacherIndex(tea_id);
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取教材数据成功");
            Map<String, Object> map = new HashMap<>();
            map.put("tsNtCategoryTeachers", tsNtCategoryTeachersForParentId);
            map.put("lock", tdNtTeacherState.getTea_islock());

            // 教学相关的介绍视频
            List<TsNtDictIndex> tsNtDictIndices = new ArrayList<>();
            TsNtDictIndex tsNtDictIndex = new TsNtDictIndex();
            tsNtDictIndex.setDict_id(15);
            tsNtDictIndex.setCode_name("/nativetalk/course-intro-video/intro.mp4");
            tsNtDictIndices.add(tsNtDictIndex);
            TsNtDictIndex tsNtDictIndexTwo = new TsNtDictIndex();
            tsNtDictIndexTwo.setDict_id(15);
            tsNtDictIndexTwo.setCode_name("/nativetalk/course-intro-video/tips.mp4");
            tsNtDictIndices.add(tsNtDictIndexTwo);
            if (version >= 2) {
                map.put("tsNtDictIndices", tsNtDictIndices);
            }
            retInfo.setObj(map);
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(), e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }


    public RetInfo findCategory() {
        String logInfo = this.getClass().getName() + ":findCategory:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            List<TsNtCategoryInfo> tsNtCategoryIndices = (List<TsNtCategoryInfo>) MemcachedUtils.get(MemcachedKey.tsNtCategoryParentsInfo);
            //   List<TsNtDictDeatil> tsNtDictDeatilList= (List<TsNtDictDeatil>) MemcachedUtils.get(MemcachedKey.tsNsDictDetail);
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取课程分类成功");
            Map<String, Object> map = new HashMap<>();
            map.put("categories", tsNtCategoryIndices);
            //   map.put("categories", tsNtDictDeatilList);
            retInfo.setObj(map);
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(), e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    public RetInfo findTeaLabel() {
        String logInfo = this.getClass().getName() + ":findCategory:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            // List<TsNtCategoryInfo> tsNtCategoryIndices = (List<TsNtCategoryInfo>) MemcachedUtils.get(MemcachedKey.tsNtCategoryParentsInfo);
            List<TsNtDictDeatil> tsNtDictDeatilList= (List<TsNtDictDeatil>) MemcachedUtils.get(MemcachedKey.tsNsDictDetail);
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取课程分类成功");
            Map<String, Object> map = new HashMap<>();
            //  map.put("categories", tsNtCategoryIndices);
            map.put("categories", tsNtDictDeatilList);
            retInfo.setObj(map);
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(), e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    public List<ExperienceCourseDto> findCourseByCategoryIdForExperience(Integer memb_id, Integer categoryId) {
        // 1.查询学生购买过体验套餐的课程ids
        List<Integer> experiencedCourseIds = courseInfoDao.findExperiencedCourseIds(memb_id);

        // 2. 根据分类查询课程信息

        if (categoryId != null && categoryId.intValue() == 0) {
            categoryId = null;
        }
        TsNtCategory category = new TsNtCategory();
        category.setCate_id(categoryId);

        List<ExperienceCourseDto> courseList = courseInfoDao.findByCateId(category);

        for (ExperienceCourseDto dto : courseList) {
            if (experiencedCourseIds.contains(dto.getCourseId())) {
                dto.setIsStudied(ExperienceCourseDto.YES);
            }
        }
        return courseList;
    }
}
